<template>
    <div>
    <basic-container>
      <avue-crud
        :option="tableOption"
        :data="tableData"
        :table-loading="tableLoading"
        :page="page"
        ref="crud"        
        
        @refresh-change="handleRefreshChange"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        @selection-change="handleSelectionChange"
        
        @search-change="handleSearchChange"
      >
      <!-- <template slot="menu" scope="scope">
        <div>
              <el-button
                  type="text"
                  @click.stop="handleResolve(scope.row)"
                  icon="el-icon-edit"
                  size="mini"
                  v-if="scope.row.status == 1 || scope.row.status == 2"
                  v-perm="'/admin/merchant_info/check'"
                >审核通过</el-button>
              <el-button
                  type="text"
                  @click.stop="handleResolve4Quit(scope.row)"
                  icon="el-icon-edit"
                  size="mini"
                  v-if="scope.row.status == 6"
                  v-perm="'/admin/merchant_info/check'"
                >审核通过</el-button>
                <el-button
                  type="text"
                  @click.stop="handleReject(scope.row)"
                  icon="el-icon-edit"
                  size="mini"
                  v-if="scope.row.status == 1 || scope.row.status == 2"
                  v-perm="'/admin/merchant_info/check'"
                >审核拒绝</el-button>
                <el-button
                  type="text"
                  @click.stop="handleReject4Quit(scope.row)"
                  icon="el-icon-edit"
                  size="mini"
                  v-if="scope.row.status == 6"
                  v-perm="'/admin/merchant_info/check'"
                >审核拒绝</el-button>
                <el-button
                  type="text"
                  @click.stop="handleRefund(scope.row)"
                  icon="el-icon-edit"
                  size="mini"
                  v-if="scope.row.status == 7"
                  v-perm="'/admin/merchant_info/quit_apply/refund'"
                >退保证金</el-button>
        </div>
      </template>
      <template slot="menuLeft">
        
          <el-button
        type="primary"
        @click="handleExport"
        :loading="downloadLoading"
        icon="el-icon-download"
        v-perm="'/admin/merchant_info/export'"
        size="small"
      >导出</el-button>

         <el-button
              v-waves
              size="small"
              class="filter-item"
              type="primary"
              icon="el-icon-edit"
              v-perm="'/admin/merchant_info/check'"
              @click="handleBatchResolve"
            >批量审核通过</el-button>
            <el-button
              v-waves
              size="small"
              class="filter-item"
              v-perm="'/admin/merchant_info/check'"
              type="danger"
              icon="el-icon-edit"
              @click="handleBatchReject"
            >批量审核拒绝</el-button>
      </template> -->
      </avue-crud>
      </basic-container>
      <!-- <el-dialog :visible.sync="quitMessageDialogShowed" title="退出申请审核" width="500px">
          <el-form :model="quitInfo" label-width="120px" v-loading="quitMessageDialogLoading">
            <el-form-item label="联系人">{{quitInfo.userName}}</el-form-item>
            <el-form-item label="联系人电话">{{quitInfo.userMobile}}</el-form-item>
            <el-form-item label="退出原因">{{quitInfo.quitReason}}</el-form-item>
            <el-form-item label="申请时间">{{quitInfo.createTime}}</el-form-item>
            <el-form-item label="银行卡号">{{quitInfo.accountNo}}</el-form-item>
            <el-form-item label="持卡人姓名">{{quitInfo.accountName}}</el-form-item>
            <el-form-item label="开户银行名称">{{quitInfo.bankName}}</el-form-item>
            <el-form-item label="申请备注">
              <el-input type="textarea" v-model="quitRemark" :rows="4" placeholder="备注"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button type="info" @click="quitMessageDialogShowed=false">取消</el-button>
              <el-button type="danger" :loading="isSubmiting" v-if="isRejecting" @click="handleSubmitReject4Quit">审核拒绝</el-button>
              <el-button type="primary" :loading="isSubmiting" v-else @click="handleSubmitResolve4Quit">审核通过</el-button>
          </div>
      </el-dialog> -->
    </div>
  </template>
  <script>
  import { validatenull } from "@/util/validate";
  import { filterForm } from "@/util/util";
  // import { DIC } from "@/const/dic";
  import {  getLabelFromDicV2 } from "@/const/dic";
  import { nowDate } from "../../util/date";
  import {dateFormat} from '../../filters/index';
  import * as apiObj from "../../api/userMerchantInfo";
  import { userMerchantInfoTableOption} from "@/const/userMerchantInfo/userMerchantInfoTableOption.js";
  
  export default {
    data() {
      return {
        bachDeleting: false,
        tableOption: {}, //表格设置属性
        tableData: [], //表格的数据
        downloadLoading:false,
        tableLoading: false,
        isSubmiting:false,
        tableQueryParam: {},
        quitRemark:'',
        isRejecting:false,
        quitInfo:{},
        quitMessageDialogLoading:false,
        quitMessageDialogShowed:false,
        page: {
          total: 0, //总页数
          currentPage: 1, //当前页数
          pageSize: 10 //每页显示多少条
        }
      };
    },
    created() {
      //初始化数据格式
      this.tableOption = userMerchantInfoTableOption;
      this.handleList();
    },
    methods: {
      handleBatchResolve(){
      if (!this.multipleSelection || !this.multipleSelection.length) {
        this.showError("请选择至少一条记录");
        return;
      }
      this.$prompt('确认审核通过所选商品吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.check({
              id:this.multipleSelection.filter(el=>el.status == 1 || el.status == 2).map(el=>el.id).join(','),
              checkStatus:4,
              remark:value.trim()
          }).then(()=>{
            return apiObj.quitApplyCheck({
              id:this.multipleSelection.filter(el=>el.status == 6).map(el=>el.userId).join(','),
              checkStatus:10,
              remark:value.trim()
            });
          }).then(() => {
            this.success();
            this.handleList();
          });
        }).catch(() => {
          
        });
    },
    handleBatchReject(){
      if (!this.multipleSelection || !this.multipleSelection.length) {
        this.showError("请选择至少一条记录");
        return;
      }
      this.$prompt('确认审核拒绝所选商品吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.check({
              id:this.multipleSelection.filter(el=>el.status == 1 || el.status == 2).map(el=>el.id).join(','),
              checkStatus:3,
              remark:value.trim()
          }).then(()=>{
            return apiObj.quitApplyCheck({
              id:this.multipleSelection.filter(el=>el.status == 6).map(el=>el.userId).join(','),
              checkStatus:5,
              remark:value.trim()
            });
          }).then(() => {
            this.success();
            this.handleList();
          });
        }).catch(() => {
          
        });
    },
    handleResolve(row) {
      let checkStatus = 4;
      if(row.status == 6) {
        checkStatus = 10;
      }
      this.$prompt('确认审核通过吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            (checkStatus == 10 ? apiObj.quitApplyCheck : apiObj.check)({
              id:checkStatus == 10 ?  row.userId : row.id,
              checkStatus,
              remark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
        }).catch(() => {
          
        });
    },
    handleRefund(row){
      this.confirm('确认退保证金吗?').then(()=>{
        apiObj.quitApplyRefund({
            id:row.userId,
            refundAmount:row.depositAmount,
            refundTime:dateFormat(new Date()),
            refundAccesssory:''
        }).then(()=>{
          this.handleList();
          this.success();
        });
      });
    },
    handleResolve4Quit(row){
      this.quitMessageDialogShowed = true;
      this.quitMessageDialogLoading = true;
      this.quitRemark = '';
      this.isRejecting = false;
      this.currentRow = row;
      apiObj.quitDetail(row.userId).then(data=>{
        this.quitInfo = data || {};
      }).finally(()=>{
        this.quitMessageDialogLoading = false;
      });
    },
    handleReject4Quit(row){
      this.quitRemark = '';
      this.quitMessageDialogShowed = true;
      this.currentRow = row;
      this.isRejecting = true;
      this.quitMessageDialogLoading = true;
      apiObj.quitDetail(row.userId).then(data=>{
          this.quitInfo = data || {};
      }).finally(()=>{
        this.quitMessageDialogLoading = false;
      });
    },
    handleSubmitReject4Quit(){
        const checkStatus = 5;
        this.isSubmiting = true;
        apiObj.quitApplyCheck({
          id:this.currentRow.userId,
          checkStatus,
          remark:this.quitRemark.trim()
        }).then(()=>{
            this.success();
            this.handleList();
            this.quitMessageDialogShowed = false;
        }).finally(()=>{
            this.isSubmiting = false;
        });
    },
    handleSubmitResolve4Quit(){
        const checkStatus = 10;
        this.isSubmiting = true;
        apiObj.quitApplyCheck({
          id:this.currentRow.userId,
          checkStatus,
          remark:this.quitRemark.trim()
        }).then(()=>{
            this.success();
            this.handleList();
            this.quitMessageDialogShowed = false;
        }).finally(()=>{
            this.isSubmiting = false;
        });
    },
    handleReject(row) {
      let checkStatus = 3;
      if(row.status == 6) {
        checkStatus = 5;
      }
      this.$prompt('确认审核拒绝吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            (checkStatus == 5 ? apiObj.quitApplyCheck : apiObj.check)({
              id:checkStatus == 5 ?  row.userId : row.id,
              checkStatus,
              remark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
          this.success();
        }).catch(() => {
          
        });
    },
      handleCurrentChange(page) {
        this.page.currentPage = page;
        this.handleList();
      },
      handleSizeChange(size) {
        this.page.pageSize = size;
        this.handleList();
      },
    
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      hasChanged(params1,params2){
        return Object.keys(params1).some(key=>params2[key] != params1[key]);
    },
    handleSearchChange(params) {
      if(this.hasChanged(params,this.tableQueryParam)){
        this.page.currentPage = 1;
      }
        this.tableQueryParam = params;
        this.handleList();
      },
            
      
      handleRefreshChange() {
        this.handleList();
      },
      /**
       * @title 获取数据
       * @detail 赋值为tableData表格即可
       *
       **/
      handleList() {
        this.tableLoading = true;
        apiObj.list({
          pageNo: this.page.currentPage,
          pageSize: this.page.pageSize,
          ...this.tableQueryParam
        })
          .then(data => {
            this.tableData = data.data || data.dataList || [];
            this.page.total = data.total;
            this.tableLoading = false;
          })
          .catch(() => {
            this.tableLoading = false;
          });
      },     
          
      handleExport() {
        this.downloadLoading = true;
        apiObj.list({
          pageNo: 1,
          pageSize: 3000,
          ...filterForm(this.$refs.crud.searchForm)
        }).then(res => {
          import("@/vendor/Export2Excel").then(excel => {
            const tHeader = ["认证状态","联系人姓名","联系人手机号码","经营品类","保证金金额","保证金支付时间","支付渠道","申请时间","审核时间","审核人","审核备注","退保证金金额","退保证金时间","创建时间","创建人","修改时间","修改人"];
            const filterVal = ["status","userName","userMobile","businessCategory","depositAmount","depositPayTime","payChannel","applyTime","checkTime","checkOperator","checkRemark","depositRefundAmount","depositRefundTime","createTime","creator","modifyTime","modifier"];
            const data = this.formatJson(filterVal, res.data || res.dataList);
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: "用户商家认证" + nowDate()
            });
            this.downloadLoading = false;
          });
        });
      },
      formatJson(filterVal, jsonData) {      
        return jsonData.map(v =>
          filterVal.map(j => {
            return getLabelFromDicV2(this.$refs.crud.DIC,j,v[j]);
          })
        );
      }
    }
  };
  </script>
